package br.inatel.ec203.projeto.view;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import br.inatel.ec203.projeto.control.ArmarioDAO;

public class LoginFrame extends JFrame{

	private static final long serialVersionUID = -6940737463680335580L;
	
	private UserDataPanel userDataPanel;
	private OkCancelButtonsPanel buttonsPanel;	
	private ArmarioDAO armarioDAO = new ArmarioDAO();
	
	public LoginFrame () {
		super("Tela de Login");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initialize();
		
		pack();
		setResizable(false); // Tamanho da tela fixo
		setLocationRelativeTo(null);
	}	

	private void initialize() {
		add(getUserDataPanel(), BorderLayout.CENTER);
		add(getButtonsPanel(), BorderLayout.PAGE_END);
	}	
	
	private UserDataPanel getUserDataPanel() {
		if (userDataPanel == null) {
			userDataPanel = new UserDataPanel();
		}
		return userDataPanel;
	}

	private OkCancelButtonsPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			buttonsPanel = new OkCancelButtonsPanel();
			buttonsPanel.addOkCancelButtonsListeners(new OkCancelButtonsListeners() {
				
				@Override
				public void okPerformed() {
					okClicked();					
				}							

				@Override
				public void cancelPerformed() {
					System.exit(0);
				}
			});
		}
		return buttonsPanel;
	}

	protected void okClicked() {
		if ((userDataPanel.getUserName().equals("admin")) && (userDataPanel.getPassWord().equals("12345"))) {
			armarioDAO.fillArrayList();
			new MainFrame().setVisible(true);
			dispose(); // Fecha a tela atual
		} else {
			JOptionPane.showMessageDialog(this, userDataPanel.getPassWord());
		}				
	}
}
